<template>
  <div class="line-detail">
    <div class="detail-head">
      <van-icon name="arrow-left" @click="goBack" />{{ id }}号线
    </div>
    <div class="detail-content">
      <div class="content-top">
        <h3>途径</h3>
        <van-cell @click="showPopup">添加订单</van-cell>
        <van-popup
          v-model="show"
          closeable
          close-icon-position="top-right"
          round
        >
          <div>
            出发站：<select class="red" v-model="info.start">
              <option
                :value="item.name"
                v-for="item in list"
                :key="item.stepsId"
              >
                {{ item.name }}
              </option>
            </select>
          </div>
          <div>
            终点站：<select class="red" v-model="info.end">
              <option
                :value="item.name"
                v-for="item in list"
                :key="item.stepsId"
              >
                {{ item.name }}
              </option>
            </select>
          </div>
          <div>车次：{{ id }}号线</div>
          <div>价钱：8元</div>

          <div>
            <van-button type="primary" @click="addBusOrderData(id)"
              >确认添加</van-button
            >
          </div>
        </van-popup>
      </div>
      <van-steps direction="vertical" :active="0">
        <van-step v-for="item in list" :key="item.stepsId">
          <p>{{ item.name }}</p>
        </van-step>
      </van-steps>
      <!-- 我的订单 -->
      <van-collapse v-model="activeNames">
        <van-collapse-item title="我的订单">
          <ul class="myOrder">
            <div class="myOrder-head">
              <p class="first">编号</p>
              <p>车次</p>
              <p>出发站</p>
              <p>抵达站</p>
              <p>支付状态</p>
            </div>
            <li
              v-for="(item, index) in orderList"
              :key="item.id"
              class="myOrder-item"
            >
              <p class="first">{{ index + 1 }}</p>
              <p>{{ item.path }}</p>
              <p>{{ item.start }}</p>
              <p>{{ item.end }}</p>
              <p v-if="item.status" class="item-paied">已支付</p>
              <p v-else class="item-prvPay" @click.stop="changFlag">
                待支付
                <pay-order
                  :orderNum="item.orderNum"
                  v-if="flag"
                  @closeFlag="closeFlag"
                ></pay-order>
              </p>
            </li>
          </ul>
        </van-collapse-item>
      </van-collapse>
    </div>
  </div>
</template>

<script>
import mixin from "@/mixin/flag";
import payOrder from "@/components/bus/PayOrder.vue";
import {
  getBusLineDetail,
  addBusOrder,
  getBusOrder,
} from "@/service/busService";

export default {
  components: { payOrder },
  mixins: [mixin],
  data() {
    return {
      show: false,
      id: "",
      flag: false, //控制支付弹窗开关
      list: [],
      orderList: [],
      activeNames: ["1"],
      info: {
        start: "",
        end: "",
      },
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    showPopup() {
      this.show = true;
    },
    changFlag() {
      console.log(666);
      this.flag = true;
    },
    closeFlag() {
      this.flag = false;
      // this.flag = !this.flag;
    },
    //添加订单
    async addBusOrderData() {
      let info = {
        ...this.info,
        price: 8,
        path: this.id + "号线",
        status: 0,
      };
      const { data } = await addBusOrder(info);
      if (data.code !== 200) return this.$toast(data.msg);
      this.$toast(`添加订单成功`);
      this.show = false;
      this.getBusOrderData();
    },
    //获取订单详情
    async getBusOrderData() {
      const { data } = await getBusOrder();
      if (data.code !== 200) return this.$toast(data.msg);
      this.orderList = data.rows;
    },
    // 获取巴士详情
    async getLineDetailDate(id) {
      const { data } = await getBusLineDetail(id);
      if (data.code !== 200) this.$toast(data.masg);
      this.list = data.rows;
    },
  },
  created() {
    this.id = this.$route.query.id;
    this.getLineDetailDate(this.id);
    this.getBusOrderData();
  },
};
</script>
<style lang="scss" scoped>
.line-detail {
  .detail-head {
    background: #fff;
    padding-left: 10px;
    padding-right: 10px;
    height: 80px;
    line-height: 80px;
    font-weight: 800;
    font-size: 20px;
    text-align: center;
    position: -webkit-sticky;
    position: sticky;
    z-index: 1;
    top: 0px;
    i {
      color: #666;
      font-size: 28px;
      position: absolute;
      left: 0;
      top: 25px;
    }
  }
  .detail-content {
    .content-top {
      display: flex;
      justify-content: space-between;
      background: #07c160;
      color: #fff;

      h3 {
        width: 100px;
        font-size: 16px;
        text-align: center;
        height: 40px;
        line-height: 40px;
        margin-left: 20px;
        box-sizing: border-box;
      }
      .van-cell {
        width: 100px;
        text-align: center;
        height: 40px;
        font-size: 16px;
        box-sizing: border-box;
        margin-right: 20px;
        background: #07c160;
        .van-cell__value {
          color: #fff;
        }
      }
      .van-popup {
        color: #333;
        width: 300px;
        height: 300px;
        box-sizing: border-box;
        padding-top: 20px;
        padding-bottom: 20px;
        font-size: 16px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        div {
          height: 40px;
          width: 200px;
          line-height: 40px;

          select {
            width: 100px;
            height: 30px;
            margin-top: 5px;
          }
          .van-button {
            width: 100%;
            .van-button__content {
              width: 100%;
            }
          }
        }
      }
    }
  }
  .myOrder {
    .myOrder-head {
      width: 100%;
      display: flex;
      justify-content: space-between;
      p {
        text-align: center;
        width: 25%;
      }
      .first {
        width: 10%;
      }
    }
    .myOrder-item {
      display: flex;
      justify-content: space-between;
      text-align: center;
      margin-top: 10px;
      height: 30px;
      p {
        text-align: center;
        line-height: 30px;
        width: 25%;
      }
      .first {
        width: 10%;
      }
      .item-paied {
        background: #07c160;
      }
      .item-prvPay {
        background: red;
      }
    }
  }
}
</style>
